package com.zhi.syc.data_applist.services;

import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;

import com.google.gson.Gson;
import com.zhi.syc.data_applist.beans.ASSettingAccountInfoBean;
import com.zhi.syc.data_applist.logger.ASLogger;
import com.zhi.syc.data_applist.util.ASUtil;

import java.util.ArrayList;
import java.util.List;

public class ASSettingAccountInfo {
    public static List<ASSettingAccountInfoBean> getAccountList(Context paramContext) {
        ArrayList<ASSettingAccountInfoBean> arrayList = new ArrayList<>();
        try {
            Cursor cursor = paramContext.getContentResolver().query(ContactsContract.Settings.CONTENT_URI, null, null, null, null);
            if (cursor == null) {
                return arrayList;
            }

            if (cursor.moveToFirst()) {
                int icaccount_type = cursor.getColumnIndex("account_type");
                int iaccount_name = cursor.getColumnIndex("account_name");

                do {
                    String str1 = ASUtil.safeString(cursor.getString(icaccount_type));
                    String str2 = ASUtil.safeString(cursor.getString(iaccount_name));

                    ASSettingAccountInfoBean bean = new ASSettingAccountInfoBean();
                    bean.setAccountType(str1);
                    bean.setAccountName(str2);
                    arrayList.add(bean);

                } while (cursor.moveToNext());
            }

            if (!cursor.isClosed()) {
                cursor.close();
                cursor = null;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return arrayList;
    }

    public static String getListZipString(Context paramContext) {
        String zipString = "";
        try {
            List<ASSettingAccountInfoBean> beans = getAccountList(paramContext);
            String result = new Gson().toJson(beans).trim();
            ASLogger.d(ASSettingAccountInfo.class.getSimpleName(), "result: " + result);
            zipString = ASUtil.stringToGZIP(result);
            beans.clear();
            beans = null;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return zipString;
    }
}
